<template>
  <div class="content">
    <van-nav-bar title="标题" left-text="返回" left-arrow @click-left="onClickLeft" />

    <van-contact-card
      type="add"
      @click.stop="$router.push('/add_address')"
      v-if="addressone.length"
    />
    <van-contact-card
      v-else
      type="edit"
      :name="addressone.name"
      :tel="addressone.tel"
      @click="onEdit"
    />
    <van-card
      v-for="item in goodList"
      :key="item.id"
      :num="item.cartnum"
      :price="item.price.toFixed(2)"
      :title="item.name"
      :thumb="item.imgs.split('||')[0]"
    />
    <van-submit-bar :price="total" button-text="支付" @submit="onSubmit" />
  </div>
</template>

<script>
import { Toast, Dialog } from "vant";
import { getAddress, getOneAddress, updateOrder } from "@api";

export default {
  data() {
    return {
      addressList: [],
      addressone: {},
      goodList: [],
      total: 0,
      local: [],
    };
  },
  created() {
    console.log(this.$route.query, 1111);

    //判断是否  是用户更换收货地址
    if (this.$route.query.id) {
      getOneAddress(this.$route.query.id).then((res) => {
        if (res.data.code === 200) {
          console.log("更换地址成功");
          console.log(res);
          this.addressone = JSON.parse(res.data.data.content);
          console.log(JSON.parse(res.data.data.content));
        }
      });
    } else {
      //若不是   请求默认地址
      getAddress().then((res) => {
        console.log(res, 666);
        if (res.data.code === 200) {
          console.log("成功获取收货地址信息");
          this.addressList = res.data.data;
          this.addressone = this.addressList[0].content;

          this.addressList.forEach((item) => {
            console.log(JSON.parse(item.content));
            if (JSON.parse(item.content).isDefault) {
              console.log("成功获取默认地址");
              this.addressone = JSON.parse(item.content);
            }
          });
          
        
         
          
        }
      });
    }
    //获取提交订单商品信息
    JSON.parse(localStorage.getItem("buycart")).forEach((item) => {
      console.log(item);
      if (item.select) {
        this.goodList.push(item);
        this.total += item.cartnum * item.price;
      }
    });
    this.total = this.total * 100;
  },
  methods: {
    onAdd() {
      this.$router.push("/ReceivingAddress");
    },
    //返回上一页
    onClickLeft() {
      this.$router.replace('/cart')
    },
    onEdit() {
      this.$router.push("/Address_list");
    },
    onSubmit() {
      
            
           
      
      Dialog.confirm({
        message: "是否下单",
      }).then((res) => {
        this.goodList.forEach((item, index) => {
          this.goodList[index].num = item.cartnum;
          this.goodList[index].addTime = new Date();
        });
        updateOrder({
          data: {
            contact: this.addressone,
            items: this.goodList,
            id: Date.now(),
          },
        })
          .then((res) => {
            if (res.data.code === 200) {
              JSON.parse(localStorage.getItem("buycart")).forEach(item=>{
           if(!item.select){
           this.local.push(item)
          }
          
       })
       if(this.local.length===0){
         localStorage.removeItem("buycart");
       }else{
         localStorage.setItem("buycart", JSON.stringify(this.local));
       }
              
              Toast.success("支付成功");
              setTimeout(() => {
                this.$router.go(-1);
              }, 500);
            }
          })
          .catch((err) => {
            Toast.fail("交易失败");
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content{
  padding-top: 46px;
}
.van-nav-bar{
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
}
</style>